package org.jackysoft.struts2.workflow;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.Collection;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.jackysoft.entity.ArchiveDocument;
import org.jackysoft.service.ArchiveDocumentService;
import org.jackysoft.util.HttpUtil;
import org.jackysoft.util.IOUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;


@Controller
@Namespace("/archivedocument")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ArchiveDocumentAction 
extends AbstractWorkflowAction<ArchiveDocument> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8795262578032597289L;
	private ArchiveDocumentService service;
	private ArchiveDocument bean;
	private Collection<ArchiveDocument> beans;
	

	public String view()throws Exception{
		bean = service.get(bean.getId());
		return SUCCESS;
	}
	
	
	public String stream() throws Exception{
		bean = service.get(bean.getId());
		byte[] content = IOUtils.toByteArray(new File(bean.getPdfFile()));
		setContentType(HttpUtil.extToMimeType("pdf"));
		setContentLength(content.length);
		inputStream = new ByteArrayInputStream(content);
		return STREAM;
	}	
		
	public void setBean(ArchiveDocument bean){
	     this.bean = bean;
	}
	public ArchiveDocument getBean(){
	     return this.bean;
	}
	public void setBeans(Collection<ArchiveDocument> beans){
	
	     this.beans = beans;
	}
	public Collection<ArchiveDocument> getBeans(){
	     return this.beans;
	}
	
	@Resource
	public void setService(ArchiveDocumentService service) {
		this.service = service;
		this.setDataProvider(service);
	}			
}